package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听器接受pre_send_sms_topic队列中的短信对象
 * @Author ztf
 * @Date 19:28 2021/11/24
 **/
@Component
@RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
@Slf4j
public class StandardSmsListener {
    /**
     * 注入策略管理者
     */
    @Autowired
    private FilterChainService filterChainService;

    /**
     * 接收mq中的消息对象让消息对象走策略模块
     * @param standardSubmit
     */
    @RabbitHandler
    public void messageHandler(StandardSubmit standardSubmit, Channel channel, Message message) throws IOException {
        log.info("【策略模块-监听器】接收队列中的短信对象，standardSubmit={}",standardSubmit);
        filterChainService.executeFilter(standardSubmit);
        // 对消息进行ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
